package com.bluedot.www.core.service;


import com.bluedot.www.core.common.HandwritingRuntimeException;
import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.BO.HandwritingFeatureBo;
import com.bluedot.www.core.pojo.DO.HandwritingFeature;
import com.bluedot.www.core.utils.DbUtil;

import java.util.List;

/**
 * <p>
 * 笔迹特征表，精确到4为小数
 * 笔迹特征：页眉留白、页脚留白、左侧留白、右侧留白、像素数、占用率、标题字体大小、标题倾斜度、标题字间距、平均字面积、最大字面积、平均行间距、标题高宽比、最小行间距、最大行间距、平均高宽比、最小高宽比、标首距、最小字面积、最大行倾角、最大高宽比、平均行倾角、均匀程度比 服务类
 * </p>
 *
 * @author zhouxuan
 * @since 2021-08-07
 */
public class HandwritingFeatureService {

    private static final String FEATURE_MAPPER = "com.bluedot.www.core.mapper.xml.HandwritingFeatureMapper.";

    private HandwritingFeatureService() {

    }

    private static HandwritingFeatureService featureService;

    private static HandwritingFeatureService getInstance() {
        if (featureService == null) {
            featureService = new HandwritingFeatureService();
        }
        return featureService;
    }

    public static Object execute(String methodName, Object[] param) {
        HandwritingFeatureService featureService = HandwritingFeatureService.getInstance();
        Object result;
        switch(methodName) {
            case "insertHandwritingFeature":
                result = featureService.insertHandwritingFeature((HandwritingFeature) param[0], (Long) param[1]);
                break;
            case "getAverageFeature":
                result = featureService.getAverageFeature((Long) param[0]);
                break;
            case "getHandwritingFeature":
                result = featureService.getHandwritingFeature((Long) param[0]);
                break;
            default:
                throw new HandwritingRuntimeException(20005, "Method NOT FOUND, method name: " + methodName);
        }
        return result;
    }

    /**
     * 增加笔迹特征
     *
     * @param handwritingFeature: 笔迹特征
     * @return boolean
     * @author He Peng
     * @date 2021/8/21 15:15
     */
    private boolean insertHandwritingFeature(HandwritingFeature handwritingFeature, Long imgId) {
        if (handwritingFeature == null) {
            return false;
        }
        Long featureId = DbUtil.generateId();
        handwritingFeature.setId(featureId);
        handwritingFeature.setGmtCreate(DbUtil.getCurrentTime());
        handwritingFeature.setGmtModified(DbUtil.getCurrentTime());
        int res1 = (int) Execute.execute(Execute.UPDATE_TYPE,
                FEATURE_MAPPER + "insertHandwritingFeature", new Object[]{handwritingFeature});
        // 更新笔迹图片的笔记特征值编号
        boolean res2 = (boolean) HandwritingImgService.execute("updateImgFeatureId", new Object[]{imgId, featureId});
        return res1 > 0 && res2;
    }


    /**
     * 更新笔迹特征
     *
     * @param handwritingFeature: 笔迹特征实体
     * @return boolean
     * @author He Peng
     * @date 2021/8/21 15:15
     */
    private boolean updateHandwritingFeature(HandwritingFeature handwritingFeature) {
        return false;
    }


    /**
     * 根据id删除笔迹特征
     *
     * @param id: 编号
     * @return boolean
     * @author He Peng
     * @date 2021/8/21 15:16
     */
    private boolean deleteHandwritingFeature(Integer id) {


        return false;
    }


    /**
     * 根据编号获取笔迹特征
     *
     * @param id: 便编号
     * @return com.bluedot.www.core.pojo.DO.HandwritingFeature
     * @author He Peng
     * @date 2021/8/21 15:17
     */
    private HandwritingFeature getHandwritingFeature(Long id) {
        List<HandwritingFeature> list = (List<HandwritingFeature>) Execute.execute(Execute.QUERY_TYPE, FEATURE_MAPPER + "getHandwritingFeature", new Object[]{id});
        return list.get(0);
    }

    /**
     * 计算得到笔迹图片的笔迹特征的平均值
     * @param reportId: 性格报告编号
     * @return com.bluedot.www.core.pojo.DO.HandwritingFeature 笔迹特征值，如果出现异常则返回空
     * @author He Peng
     * @date 2021/10/21 0:06
     */
    private HandwritingFeatureBo getAverageFeature(Long reportId) {
        List<HandwritingFeatureBo> featureList = (List<HandwritingFeatureBo>) Execute.execute(Execute.QUERY_TYPE,
                FEATURE_MAPPER + "getAverageFeature", new Object[]{reportId});
        return featureList != null && featureList.size() > 0 ? featureList.get(0) : null;
    }
}
